﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonUtils;
using EntityDataModel;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class FriendshipBLL
    {
        public static String FriendshipRequest
        {
            get { return FriendshipDAL.FriendshipRequest; }
            set { FriendshipDAL.FriendshipRequest = value; }
        }

        public static String FriendshipRequestAccepted
        {
            get { return FriendshipDAL.FriendshipRequestAccepted; }
            set { FriendshipDAL.FriendshipRequestAccepted = value; }
        }
        /// <summary>     
        /// Get all friends for the given username
        /// </summary>
        public static List<User> GetAllFriendsForUser(String username)
        {
            try
            {
                return FriendshipDAL.GetAllFriendsForUser(username.Trim());
            }
            catch (Exception e)
            {
                System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                LogWriter.AddLog(e, LogStatus.Error, BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name);
                throw new Exception(sf.GetMethod().Name + " unexpected error", e);
            }
        }

        /// <summary>     
        /// Check if two given user are friends
        /// </summary>
        public static Boolean AreTheyFriends(String userName1, String userName2)
        {
            try
            {
                return FriendshipDAL.AreTheyFriends(userName1, userName2);
            }
            catch (Exception e)
            {
                System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                LogWriter.AddLog(e, LogStatus.Error, BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name);
                throw new Exception(sf.GetMethod().Name + " unexpected error", e);
            }
        }

        /// <summary>     
        /// Create a friendship between two users
        /// </summary>
        public static Boolean CreateFriendship(String userName1, String userName2)
        {
            try
            {
                return FriendshipDAL.CreateFriendship(userName1, userName2);
            }
            catch (Exception e)
            {
                System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                LogWriter.AddLog(e, LogStatus.Error, BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name);
                throw new Exception(sf.GetMethod().Name + " unexpected error", e);
            }
        }

        /// <summary>     
        /// Accept a friendship request
        /// </summary>
        public static void AcceptFriendship(String messageId)
        {
            try
            {
                FriendshipDAL.AcceptFriendship(MessageDAL.GetMessageById(messageId));
            }
            catch (Exception e)
            {
                System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                LogWriter.AddLog(e, LogStatus.Error, BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name);
                throw new Exception(sf.GetMethod().Name + " unexpected error", e);
            }
        }

        /// <summary>     
        /// Reject a friendship request
        /// </summary>
        public static void RejectFriendship(String messageId)
        {
            try
            {
                FriendshipDAL.RejectFriendship(MessageDAL.GetMessageById(messageId));
            }
            catch (Exception e)
            {
                System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                LogWriter.AddLog(e, LogStatus.Error, BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name);
                throw new Exception(sf.GetMethod().Name + " unexpected error", e);
            }
        }
    }
}
